define(['jsonpack', './../initial', './LocationPoints'], function (jsonpack) {
    var app = angular.module('App');
    app.factory('Project.MonitorPoints', ['$http', '$q', 'LocationPoints', function ($http, $q, LocationPoints) {
        var Extend = {};

        Extend._getChanges = function (queryParams) {
            queryParams.isDesign = typeof (queryParams.isDesign) === 'undefined' ? false : queryParams.isDesign;
            var defer = $q.defer();
            var _this = this;
            var lpts = _this.findLocationPoints(queryParams.type, queryParams.location);
            lpts.getRecentChanges(queryParams)
                .then(function (data) {
                    lpts.noData = data.curValues.length === 0;
                    lpts.changes = data;
                    defer.resolve(data);
                });
            return defer.promise;
        };

        Extend.getRecentChanges = function (queryParams) {
            var _this = this;
            queryParams.prjId = this.prjId;
            // queryParams.isDesign = typeof (queryParams.isDesign) === 'undefined' ? false : queryParams.isDesign;
            if (queryParams.type === '收敛') {//对于收敛要多查询一个同设计值相比最大变化点
                return _this._getRecentDesignChanges(queryParams);
            } else {
                return _this._getChanges(queryParams);
            }
        };

        Extend._getRecentDesignChanges = function (queryParams) {
            var _this = this;
            var defer = $q.defer();
            var lpts = _this.findLocationPoints(queryParams.type, queryParams.location);
            var p1 = $.extend({ isDesign: false }, queryParams);
            var p2 = $.extend({ isDesign: true }, queryParams);
            $q.all([_this._getChanges(p1), _this._getChanges(p2)])
                .then(function (chordDatas) {
                    var data = chordDatas[0];
                    lpts.noData = data.curValues.length === 0;
                    data.maxDesignPointAllTheTime = chordDatas[1].maxPointAllTheTime;
                    lpts.changes = data;
                    defer.resolve(data);
                });
            return defer.promise;
        };
        /** 获取该项目的所有监测点 */
        Extend.getMonitorPoints = function () {
            var defer = $q.defer();
            var _this = this;
            if (this.monitorPoints.length > 0) {
                defer.resolve(this.monitorPoints);
            } else {
                $http({
                    url: '/metro/project/monitorPoints',
                    params: {
                        prjId: _this.prjId
                    }
                })
                    .success(function (data) {
                        data.forEach(function (typePoints, index1) {
                            typePoints.locationPoints.forEach(function (locationPts, index2) {
                                locationPts.index = index2;
                                if (index1 === 0 && index2 === 0) locationPts.active = true;// 默认第一个location为active
                                locationPts = $.extend(new LocationPoints(), locationPts);
                                locationPts.checkIsRingNumberConfig();//查看是否配置了环号
                                locationPts.checkIsMileageConfig();//查看是否配置了里程
                                typePoints.locationPoints[index2] = locationPts;
                            });
                        });
                        _this.monitorPoints = data;
                        defer.resolve(_this.monitorPoints);
                    });
            }
            return defer.promise;
        };
        /** 查找location points */
        Extend.findLocationPoints = function (monitorType, location) {
            var ll = null;
            if (!monitorType) {
                this.monitorPoints.some(function (typePoints) {
                    return typePoints.locationPoints.some(function (locationPts) {
                        if (locationPts.active) {
                            ll = locationPts;
                            return true;
                        } else {
                            return false;
                        }
                    })
                })
            } else {
                for (var i = 0; i < this.monitorPoints.length; i++) {
                    if (this.monitorPoints[i].monitorType === monitorType) {
                        var lpts = this.monitorPoints[i].locationPoints;
                        for (var j = 0; j < lpts.length; j++) {
                            if (lpts[j].location === location) {
                                ll = lpts[j];
                                break;
                            }
                        }
                        break;
                    }
                }
            }
            return ll;
        }
        return Extend;
    }]);
});

